import styles from './index.module.less'
import { getLocalStorage } from '@/storage'
import { useEffect, useState } from 'react'
import { history } from 'umi'
import { ReportContent } from './components/ReportContent'
import { isInWechatMiniProgram } from '@/utils/wechatHelper'
import type { RoleType } from '../generate/interface'
import type { CommonReportData } from './interface'
import type { ReportType } from '../../entry/interface'
import wx from 'weixin-webview-jssdk'

export const ShareReport = () => {
    const queryParams = history.location.query as { role_type: RoleType; report_type: ReportType }
    const roleType = queryParams.role_type
    // const reportType = queryParams.report_type

    const [reportData, setReportData] = useState<CommonReportData>()

    useEffect(() => {
        switch (roleType) {
            case 'department':
                setReportData(getLocalStorage('DEPARTMENT_REPORT_DATA'))
                break
            case 'trainer':
                setReportData(getLocalStorage('TRAINER_REPORT_DATA'))
                break
            case 'teacher':
                setReportData(getLocalStorage('TEACHER_REPORT_DATA'))
                break
        }
    }, [roleType])

    // 由于马兰花系统拆分涉及站点登录问题，避免查看我的档案跳转登录页面进行登录
    // const onViewMyReport = () => {
    //     const isLogin = getLocalStorage('ACCESS_TOKEN')
    //     if (isLogin) {
    //         history.push({ pathname: '/report/entry' })
    //     } else {
    //         history.push({ pathname: '/login' })
    //     }
    // }

    const onShare = () => {
        if (isInWechatMiniProgram()) {
            const site = getLocalStorage('SITE_DOMAIN')
            const secret = getLocalStorage('SHARE_SECRET')
            const token = getLocalStorage('ACCESS_TOKEN')
            const redirectUrl = `/pages/reportshare/reportshare?site=${site}&token=${token}&secret=${secret}`

            wx.miniProgram.redirectTo({
                url: redirectUrl,
            })
        } else {
            history.push({ pathname: '/report/share/guide' })
        }
    }
    return (
        <div className={styles.page}>
            <div className={styles.container}>
                <ReportContent type={roleType} data={reportData} />
            </div>

            <div className={styles.page_footer}>
                <div className={styles.operate}>
                    {/* 由于马兰花系统拆分涉及站点登录问题，避免查看我的档案跳转登录页面进行登录 */}
                    {/* {reportType === 'other' && (
                        <div className={styles.operate_view} onClick={onViewMyReport} />
                    )} */}

                    <div className={styles.operate_share} onClick={onShare} />
                </div>

                <p>数据来源：杭州沃土教育科技股份有限公司</p>
            </div>
        </div>
    )
}

export default ShareReport
